package com.polaris.live.popup

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.CollectionUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.dialog.GlobalDialogManager
import com.polaris.live.im.sfs.bean.live.ext.UserBasicVo
import com.polaris.live.interface_bag.XpopupCallBackImpl
import com.polaris.live.manager.LiveStateManager
import com.polaris.live.net.liveService
import com.polaris.live.popup.party.PartyTaskPopup
import com.polaris.live.popup.userprofile.AnchorUserProfilePopup
import com.polaris.live.popup.userprofile.PartyAnchorUserProfilePopup
import com.polaris.live.popup.userprofile.PartyUserProfilePopup
import com.polaris.live.popup.userprofile.UserProfilePopup
import com.polaris.live.resp.back_resp.InfoOtherResp
import com.polaris.live.ui.party.widget.seat.bean.BaseSeatBean
import com.polaris.live.utils.XPopupUtils
import com.polaris.live.viewmodel.GlobalViewModelProxy
import com.polaris.live.viewmodel.party.PartyAnchorFragmentModel

/**
 * 全局popup管理类   这里的类不想强硬的让你把弹窗都写到这里 ， 只是你如果想写就写  不想写就不写  图一个方便
 *
 * @author Created by 半仙 on 2023/8/17/017 16:30
 */
object GlobalPopupManager {

    //完善资料popup
    fun showCompletePopup(
        context: Context,
        isSetSex: () -> Unit = {},
        isOk: () -> Unit,
    ) {
        //先判断是否有性别
        if (UserManager.getUser().sex == AppConst.UserSex.NONE) {
            val block: () -> Unit = {
//                val trafficSourceType = UserManager.getUser().flag?.trafficSourceType
//                val isTraffic =
//                    UserManager.getUser().sex == AppConst.UserSex.WOMAN && trafficSourceType != null && trafficSourceType > 0
//                if (isTraffic) {
//                    GlobalDialogManager.showNewLiving(context) {
//                        if (!it) {
//                            isSetSex.invoke()
//                            isOk.invoke()
//                        } else {
//                            return@showNewLiving
//                        }
//                    }
//                } else {
                isSetSex.invoke()
                isOk.invoke()
//                }
            }

            XPopup.Builder(context)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .asCustom(CompleteGenderDialog(context, block))
                .show()
        } else {
            isOk.invoke()
        }
    }

    /**
     * 显示主播聊天室用户卡片
     */
    fun showPartyAnchorUserProfilePopup(
        context: Context,
        owner: LifecycleOwner,
        viewModel: PartyAnchorFragmentModel,
        otherId: Long,
        page: Int,
        objectId: Long? = null,
        seatBean: BaseSeatBean?,
        banMessageUser: MutableList<Long>?,
        onLeaveSeatClickListener: (() -> Unit)? = null,
        onMessageClickListener: ((infoOtherResp: InfoOtherResp) -> Unit)? = null,
        onFollowerClickListener: (() -> Unit)? = null,
        onGiftClickListener: ((infoOtherResp: InfoOtherResp) -> Unit)? = null,
        onSelectMoreCardListener: ((infoOtherResp: InfoOtherResp) -> Unit)? = null,
    ) {
        GlobalViewModelProxy.getInfoUser(viewModel, otherId) { resp ->
            XPopupUtils.showPopup(
                PartyAnchorUserProfilePopup(
                    context,
                    viewModel,
                    resp,
                    page,
                    objectId,
                    seatBean,
                    banMessageUser,
                    onLeaveSeatClickListener,
                    onMessageClickListener,
                    {
                        onFollowerClickListener?.invoke()
                    },
                    onGiftClickListener,
                    onSelectMoreCardListener
                ),
                {
                    isRequestFocus(false)
                },
                lifecycle = owner.lifecycle
            )
        }
    }

    /**
     * 显示聊天室用户卡片
     */
    fun showPartyUserProfilePopup(
        context: Context,
        owner: LifecycleOwner,
        viewModel: BaseViewModel,
        otherId: Long,
        page: Int,
        objectId: Long? = null,
        mAnchorId: Long?,
        seatBean: BaseSeatBean?,
        onMessageClickListener: ((mInfoOtherResp: InfoOtherResp) -> Unit)? = null,
        onFollowerClickListener: (() -> Unit)? = null,
        onLeaveSeatClickListener: (() -> Unit)? = null,
        onGiftClickListener: ((infoOtherResp: InfoOtherResp) -> Unit)? = null,
        onSelectMoreCardListener: ((infoOtherResp: InfoOtherResp) -> Unit)? = null,
    ) {
        GlobalViewModelProxy.getInfoUser(viewModel, otherId) { resp ->
            XPopupUtils.showPopup(
                PartyUserProfilePopup(
                    context,
                    viewModel,
                    resp,
                    page,
                    objectId,
                    mAnchorId,
                    seatBean,
                    onMessageClickListener?.run {
                        { invoke(resp) }
                    }, {
                        onFollowerClickListener?.invoke()
                    }, onLeaveSeatClickListener,
                    onGiftClickListener,
                    onSelectMoreCardListener
                ),
                {
                    isRequestFocus(false)
                },
                lifecycle = owner.lifecycle
            )
        }
    }

    /**
     * 主播用户通用卡片
     */
    fun showAnchorUserProfilePopup(
        context: Context,
        owner: LifecycleOwner,
        viewModel: BaseViewModel,
        otherId: Long,
        page: Int,
        objectId: Long? = null,
        banMessageUser: MutableList<Long>? = null,
        onMessageClickListener: ((userInfo: InfoOtherResp) -> Unit)? = null,
        onFollowerClickListener: (() -> Unit)? = null,
        onSelectMoreCardListener: ((infoOtherResp: InfoOtherResp) -> Unit)? = null,
    ) {
        GlobalViewModelProxy.getInfoUser(viewModel, otherId) { resp ->
            XPopupUtils.showViewPopup(
                AnchorUserProfilePopup(
                    context,
                    viewModel,
                    resp,
                    page,
                    objectId,
                    banMessageUser,
                    onMessageClickListener?.run {
                        { invoke(resp) }
                    }, {
                        onFollowerClickListener?.invoke()
                    }, onSelectMoreCardListener
                ),
                lifecycle = owner.lifecycle
            )
        }
    }

    /**
     * 用户通用卡片
     */
    fun showUserProfilePopup(
        context: Context,
        owner: LifecycleOwner,
        viewModel: BaseViewModel,
        otherId: Long,
        page: Int,
        objectId: Long? = null,
        goneBottom: Boolean? = null,
        onMessageClickListener: ((userInfo: InfoOtherResp) -> Unit)? = null,
        onFollowerClickListener: (() -> Unit)? = null,
        onSelectMoreCardListener: ((infoOtherResp: InfoOtherResp) -> Unit)? = null,
        onGiftClickListener: ((infoOtherResp: InfoOtherResp) -> Unit)? = null,
    ) {
        GlobalViewModelProxy.getInfoUser(viewModel, otherId) { resp ->
            XPopupUtils.showViewPopup(
                UserProfilePopup(
                    context,
                    viewModel,
                    resp,
                    page,
                    objectId,
                    goneBottom ?: false,
                    onMessageClickListener?.run {
                        { invoke(resp) }
                    },
                    {
                        onFollowerClickListener?.invoke()
                    },
                    onSelectMoreCardListener,
                    onGiftClickListener
                ),
                lifecycle = owner.lifecycle
            )
        }
    }

    //=================================================充值半弹窗=================================================================
    private var isShowRecharge = false
    fun isShowRecharge(): Boolean {
        return isShowRecharge
    }

    fun showRechargePopup(
        context: Context,
        viewModel: BaseViewModel,
        detail: String? = null,
        callTime: Long? = null,
        onRechargeOk: ((type: Boolean?) -> Unit)? = null,
        isNeedBottom: Boolean,
        isNeedShowWeekPack: Boolean = false,
        luckyBagCoin: Long? = null,
    ) {
        if (isShowRecharge) {
            return
        }

        isShowRecharge = true
        GlobalViewModelProxy.getRechargeList(viewModel, {
            isShowRecharge = false
        }) {
            XPopup.Builder(context)
                .hasShadowBg(false)
                .enableDrag(false)
                .dismissOnTouchOutside(false)
                .isDestroyOnDismiss(true)
                .setPopupCallback(object : XpopupCallBackImpl {
                    override fun onDismiss(popupView: BasePopupView) {
                        showTodayRecharge(viewModel, onRechargeOk, isNeedShowWeekPack)
                        super.onDismiss(popupView)
                    }

                    override fun onCreated(popupView: BasePopupView) {
                        super.onCreated(popupView)
                        isShowRecharge = true
                    }
                })
                .asCustom(
                    RechargePopup(context, viewModel, it, detail, callTime,
                        onRechargeBack = { type ->
                            onRechargeOk?.invoke(type)
                        }, onDestroyCallback = {
                            LiveStateManager.updateRechargeState(false)
                            isShowRecharge = false
                        }, isNeedBottom, luckyBagCoin
                    )
                )
                .show()
        }
    }

    /**
     *     今日特惠充值弹窗
     *     这个type 如果 等于null  代表充值列表没有进行充值
     *             如果 等于true  代表充值成功成功了
     *             如果 等于false 代表今日特价也没有充值  或者  没有今日特价的权限
     *     总的来说就是需要先记录第一次返回的值 如果是true 充值成功 直接做你的操作就好
     *     如果是null 那么就需要看下有没有今日特价 如果没有今日特价或者今日特价也没充值 就会返回false
     */
    fun showTodayRecharge(
        viewModel: BaseViewModel,
        onRechargeOk: ((type: Boolean) -> Unit)? = null,
        isNeedShowWeekPack: Boolean = false,
    ) {
        GlobalViewModelProxy.getTodayRecharge(viewModel) { state ->
            if (state is ResultState.Success) {
                val data = state.data
                if (CollectionUtils.isEmpty(data)) {
                    if (isNeedShowWeekPack) {
                        GlobalDialogManager.showWeekPackPrice(viewModel, onRechargeOk)
                    } else {
                        onRechargeOk?.invoke(false)
                    }
                    return@getTodayRecharge
                }
                val topActivity = ViewUtils.getAliveTopActivity()
                    ?: return@getTodayRecharge

                GlobalDialogManager.showRechargeDialog(
                    topActivity,
                    state.data,
                    onRechargeOk
                ) {
                    showTodayRecharge(viewModel, onRechargeOk, isNeedShowWeekPack)
                }
            } else {
                if (isNeedShowWeekPack) {
                    GlobalDialogManager.showWeekPackPrice(viewModel, onRechargeOk)
                } else {
                    onRechargeOk?.invoke(false)
                }
            }
        }
    }

    fun showReport(
        context: Context,
        nickname: String,
        viewModel: BaseViewModel,
        otherId: Long,
        objectId: Long? = null,
        owner: LifecycleOwner? = null,
    ) {
        XPopupUtils.showPopup(
            ReportSourcePopup(
                context,
                owner,
                nickname,
                viewModel,
                otherId,
                objectId
            ),
            {
                hasShadowBg(true)
            },
            lifecycle = owner?.lifecycle
        )
    }

    fun showOptionReport(
        context: Context,
        owner: LifecycleOwner?,
        viewModel: BaseViewModel,
        nickname: String,
        otherId: Long,
        sourceType: Int,
        objectId: Long?,
    ) {
        XPopupUtils.showPopup(
            ReportOptionPopup(
                context,
                viewModel,
                nickname,
                otherId, sourceType, objectId
            ),
            {
                hasShadowBg(true)
            },
            lifecycle = owner?.lifecycle
        )
    }

    /**
     * 用户在线列表
     */
    fun showOnlineUser(
        context: Context,
        owner: LifecycleOwner,
        viewModel: BaseViewModel,
        liveId: Long,
        homeOwner: Long,
        liveType: Int,
        administrators: List<Long>? = null,
        onHeadClickListener: ((otherId: Long) -> Unit)? = null,
        onSendGiftSomeOneCallback: ((otherId: UserBasicVo?) -> Unit)? = null,
    ) {
        GlobalViewModelProxy.getOnlineUser(viewModel, liveId) {
            XPopupUtils.showViewPopup(
                OnlineAudiencePopup(
                    context,
                    homeOwner,
                    it,
                    liveType,
                    administrators,
                    onHeadClickListener,
                    onSendGiftSomeOneCallback
                ),
                {
                    hasShadowBg(true)
                },
                lifecycle = owner.lifecycle
            )
        }
    }

    /**
     * 房间在线用户
     */
    fun showOnRoomUser(
        context: Context,
        owner: LifecycleOwner,
        viewModel: BaseViewModel,
        liveId: Long,
        homeOwner: Long,
        liveType: Int,
        administrators: List<Long>? = null,
        onHeadClickListener: ((otherId: Long) -> Unit)? = null,
        onSendGiftSomeOneCallback: ((otherId: UserBasicVo?) -> Unit)? = null,
    ) {
        XPopupUtils.showViewPopup(
            RoomUserOnLinePopup(
                context,
                viewModel,
                liveId,
                homeOwner,
                liveType,
                administrators,
                onHeadClickListener,
                onSendGiftSomeOneCallback
            ),
            {
                enableDrag(false)
                navigationBarColor(com.polaris.live.R.color.white)
                hasShadowBg(false)
            },
            lifecycle = owner.lifecycle
        )
    }

    /**
     * 房间任务弹窗
     */
    fun showTaskPopup(
        context: Context,
        viewModel: BaseViewModel,
        pageType: Int,
    ) {
        GlobalViewModelProxy.getTaskData(viewModel, pageType) {
            XPopup.Builder(context)
                .isViewMode(true)
                .isDestroyOnDismiss(true)
                .asCustom(
                    PartyTaskPopup(
                        context,
                        it,
                        viewModel,
                        pageType
                    )
                )
                .show()
        }
    }

    /**
     * 游戏币兑换弹窗
     */
    fun showExchangeGamePopup(context: Context, viewModel: BaseViewModel) {
        viewModel.request({ liveService.getPartyGameCoin() }, {
            XPopup.Builder(context)
                .enableDrag(false)
                .moveUpToKeyboard(false)
                .isDestroyOnDismiss(true)
                .asCustom(ExchangeGamePopup(context, viewModel, it))
                .show()
        })
    }

    /**
     * 派对更多弹窗
     */
    fun showSelectMoreCard(
        context: Context,
        owner: LifecycleOwner,
        viewModel: BaseViewModel,
        infoUserResp: InfoOtherResp,
        mutableSeatBean: BaseSeatBean?,
        banMessageUser: List<Long>,
        administrators: List<Long>,
        objectId: Long?,
        homeOwnerId: Long?,
        mVideoRoom: Boolean,
        onInviteOrKickSiteListener: ((isInviteUp: Boolean) -> Unit)? = null,
        onKickRoomListener: (() -> Unit)? = null,
        onAtClickListener: (() -> Unit)? = null,
    ) {
        XPopupUtils.showViewPopup(
            SelectMoreCardPopup(
                context,
                viewModel,
                infoUserResp,
                mutableSeatBean,
                banMessageUser,
                administrators,
                objectId,
                homeOwnerId,
                mVideoRoom,
                onInviteOrKickSiteListener,
                onKickRoomListener,
                onAtClickListener
            ),
            {
                isRequestFocus(false)
            },
            lifecycle = owner.lifecycle
        )
    }

    fun showLiveSelectMoreCard(
        context: Context,
        owner: LifecycleOwner,
        viewModel: BaseViewModel,
        infoOtherResp: InfoOtherResp,
        objectId: Long?,
        homeOwnerId: Long?,
        banMessageUser: List<Long>? = null,
        isCall: Boolean = true,
        mOnKickRoomListener: (() -> Unit)? = null,
        onAtClickListener: (() -> Unit)? = null,
    ) {
        XPopupUtils.showViewPopup(
            LiveSelectMoreCardPopup(
                context,
                viewModel,
                infoOtherResp,
                objectId,
                homeOwnerId,
                banMessageUser,
                isCall,
                mOnKickRoomListener = mOnKickRoomListener,
                mOnAtClickListener = onAtClickListener
            ),
            {
                isRequestFocus(false)
            },
            lifecycle = owner.lifecycle
        )
    }
}